package com.example.leetdemo.finash;

public class Solution1277 {

    public int countSquares(int[][] matrix) {
        int dp[][] = new int[matrix.length + 1][matrix[0].length + 1];
        int sum = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == 1) {
                    dp[i + 1][j + 1] = Math.min(Math.min(dp[i][j + 1], dp[i + 1][j]), dp[i][j]) + 1;
                    sum += dp[i + 1][j + 1];
                }
            }
        }
        return sum;
    }

}
